package com.szzg10.mina2.chapter9;

import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

import javax.swing.JFrame;
import javax.swing.JPanel;

import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IoSession;

public class ImageClientHandler extends IoHandlerAdapter {

	@Override
	public void messageReceived(IoSession session, Object message)
			throws Exception {
		
		ImageResponse response = (ImageResponse)message;
		showImageResponse(response);
	}
	
	private void showImageResponse(ImageResponse response) {
		
		final BufferedImage image1 = response.getImage1();
		final BufferedImage image2 = response.getImage2();
		
		JFrame frame = new JFrame();
		frame.setExtendedState(Frame.MAXIMIZED_BOTH);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JPanel panel = new JPanel() {
			@Override
			protected void paintComponent(Graphics g) {
				Graphics2D g2 = (Graphics2D)g;
				g2.drawImage(image1, 0, 0, image1.getWidth(), image2.getHeight(), null);
				g2.drawImage(image2, image1.getWidth(), image1.getHeight(), image2.getWidth(), image2.getHeight(), null);
			}
		};
		
		frame.getContentPane().add(panel);
		frame.setVisible(true);
	}
}
